/*
 * IndexPage.h
 *
 *  Created on: 29/09/2010
 *      Author: paulo
 */

#ifndef INDEXPAGE_HPP_
#define INDEXPAGE_HPP_

#include <stdlib.h>
#include "Page.hpp"
#include "WeakPointer.hpp"
#include "SmartPointer.hpp"


template<class Key, class DataRef, class Compare=less<Key> >

class IndexPage:public Page<Key, DataRef,Compare>
{

public:
	typedef Key 								key_type;
	typedef Compare 							compare_type;
	typedef std::vector<Key>					Key_Container;
	typedef std::vector<PagePtr_t>				Container;
	typedef Container::value_type				value_type;
	typedef Key_Container::iterator				key_iterator;
	typedef Container::iterator					iterator;
	typedef	Container::difference_type			difference_type;
	typedef Container::size_type				size_type;
	typedef SmartPointer<page,CacheType>		PagePtr_t;
	typedef SmartPointer<indexPage,CacheType>	iPagePtr_t;
	typedef SmartPointer<indexPage,CacheType>	lPagePtr_t;


protected:
	WeakPointer<indexPage, CacheType>		parent;
	WeakPointer<indexPage, CacheType>		rlink;
	WeakPointer<indexPage, CacheType>		lLink;
	Container		entries;
	Container		children;
	Key_Container	keys;


public:
	IndexPage();
	IndexPage(iPagePtr_t p, Key_Container k, Container c);
	template<class InputIterator>
	IndexPage(InputIterator f, InputIterator l);
	virtual ~IndexPage(){};
	IndexPage& operator=(const key_type& k);
	virtual bool empty();
	virtual size_type size();
	virtual size_type count(const key_type& k);
	iterator find(const value_type& v);
	iterator find(const key_type& k);
	iterator begin();
	iterator end();
	iterator insert(const value_type& v);
	iterator insert(key_type& k, const value_type& v);
	iterator insert(iterator pos, key_type& k, const value_type& v);
	void insert(InputIterator f, InputIterator l);
	void erase(iterator pos);
	void erase(iterator f, iterator l);
	size_type erase(const key_type& k);
	iterator lower_bound(const key_type& k);
	iterator upper_bound(const key_type& k);
	std::pair<iterator,iterator> equal_range(const key_type& k);
	virtual bool isLeaf();
	virtual iPagePtr_t getIndexPage();
	virtual key_type& getPageKey();
	key_type& key(iterator i);
	virtual iPagePtr_t getParent();
	virtual void setParent(iPagePtr_t ip);
	virtual bool isFull();
	iPagePtr_t getrSibling();
	iPagePtr_t getlSibling();
	bool merge(iPagePtr_t p);
	std::pair<key_tpe,IpagePtr_t> split();
	std::pair<State, iPagePtr> borrow();

}; //fin clase IndexPage

#endif /* INDEXPAGE_HPP_ */

